package cn.bass.marketing.service.strategyPrize.impl;

import cn.bass.marketing.common.constant.RedisConstant;
import cn.bass.marketing.common.enums.StatusEnums;
import cn.bass.marketing.common.utils.json.JacksonUtil;
import cn.bass.marketing.common.vo.resp.MarketingStrategyPrizeRespVO;
import cn.bass.marketing.convertor.MarketingStrategyPrizeConvertor;
import cn.bass.marketing.entity.MarketingStrategyPrizeDO;
import cn.bass.marketing.mapper.MarketingStrategyPrizeMapper;
import cn.bass.marketing.redis.core.IRedisService;
import cn.bass.marketing.service.strategyPrize.MarketingStrategyPrizeService;
import cn.hutool.core.collection.CollUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import lombok.extern.slf4j.Slf4j;
import org.redisson.api.RMap;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Slf4j
@Service
public class MarketingStrategyPrizeServiceImpl extends ServiceImpl<MarketingStrategyPrizeMapper, MarketingStrategyPrizeDO> implements MarketingStrategyPrizeService {

    @Resource
    private IRedisService redisService;

    @Override
    public List<MarketingStrategyPrizeRespVO> selectEnableStrategyPrizeList(String strategyId) {
        log.info("查询策略奖品列表，策略ID:{}", strategyId);
        // 从缓存中获取
        String redisStrategyKey = String.format(RedisConstant.MARKETING_ACTIVITY_STRATEGY, strategyId);
        List<MarketingStrategyPrizeRespVO> cacheStrategyPrizeList = redisService.getValue(redisStrategyKey);
        if (CollUtil.isNotEmpty(cacheStrategyPrizeList) && !cacheStrategyPrizeList.isEmpty()) {
            return cacheStrategyPrizeList;
        }
        // 缓存中不存在，从数据库中获取
        LambdaQueryWrapper<MarketingStrategyPrizeDO> queryWrapper = Wrappers.lambdaQuery();
        queryWrapper.eq(MarketingStrategyPrizeDO::getCStrategyId, strategyId);
        queryWrapper.eq(MarketingStrategyPrizeDO::getCStatus, StatusEnums.ENABLED.getStatusType());
        List<MarketingStrategyPrizeDO> marketingStrategyPrizeDos = this.baseMapper.selectList(queryWrapper);
        // 将查询结果放入缓存
        List<MarketingStrategyPrizeRespVO> marketingStrategyPrizeRespVos = MarketingStrategyPrizeConvertor.INSTANCE.doList2RespVOList(marketingStrategyPrizeDos);
        redisService.setValue(redisStrategyKey, marketingStrategyPrizeRespVos);
        return marketingStrategyPrizeRespVos;
    }

    @Override
    public void saveCacheStrategyPrizeRangeMap(String strategyId, Integer rateRange, Map<Integer, Integer> strategyPrizeRangeMap) {
        log.info("缓存策略奖品区间，策略ID:{}, 奖品区间:{}, 奖品区间:{}", strategyId, rateRange, strategyPrizeRangeMap);
        String redisStrategyPrizeRangeKey = String.format(RedisConstant.MARKETING_ACTIVITY_STRATEGY_PRIZE_RANGE, strategyId);
        log.info("缓存策略奖品区间，redis key:{}, 奖品区间:{}", redisStrategyPrizeRangeKey, rateRange);
        redisService.setValue(redisStrategyPrizeRangeKey, rateRange);

        // 存储概率查找表
        String prizesRangeMapKey = String.format(RedisConstant.MARKETING_ACTIVITY_STRATEGY_PRIZE_RANGE_MAP, strategyId);
        log.info("缓存策略奖品区间，redis key:{}, 奖品区间:{}", prizesRangeMapKey, JacksonUtil.toJsonString(strategyPrizeRangeMap));
        RMap<Object, Object> redisServiceMap = redisService.getMap(prizesRangeMapKey);
        redisServiceMap.putAll(strategyPrizeRangeMap);
    }

    @Override
    public Integer getCacheRateRange(String strategyId) {
        log.info("获取缓存的奖品区间，策略ID:{}", strategyId);
        String redisStrategyPrizeRangeKey = String.format(RedisConstant.MARKETING_ACTIVITY_STRATEGY_PRIZE_RANGE, strategyId);
        Integer rateRange = redisService.getValue(redisStrategyPrizeRangeKey);
        if (rateRange != null) {
            return rateRange;
        }
        log.error("缓存中不存在奖品区间，策略ID:{}", strategyId);
        return 0;
    }

    @Override
    public Integer getCacheStrategyPrizeRangeMap(String strategyId, Integer rateRange) {
        String prizesRangeMapKey = String.format(RedisConstant.MARKETING_ACTIVITY_STRATEGY_PRIZE_RANGE_MAP, strategyId);
        return redisService.getFromMap(prizesRangeMapKey, rateRange);
    }
}
